import * as actionTypes from "./constants";

import {
  getTopBanners,
  getHotRecommends,
  getTopList
} from "@/services/recommend"
import { getNewAlbums } from "@/services/recommend";

const changeTopBannerAction=(res)=>({
type:actionTypes.CHANGE_TOP_BANNERS,
topBanners:res.banners
})
  
const changeHotRecommendAction=(res)=>({
  type:actionTypes.CHANGE_HOT_RECOMMEND,
  hotRecommends:res.result
})

const changeNewAlbumAction=(res)=>({
  type:actionTypes.CHANGE_NEW_ALBUM,
  newAlbums: res.albums
})

const changeUpRankingAction = (res) => ({
  type: actionTypes.CHANGE_UP_RANKING,
  upRanking: res
})

const changeNewRankingAction = (res) => ({
  type: actionTypes.CHANGE_NEW_RANKING,
  newRanking: res
})

const changeOriginRankingAction = (res) => ({
  type: actionTypes.CHANGE_ORIGIN_RANKING,
  originRanking: res
})

// 执行时用 dispatch(getTopBannerAction()) 执行一下  调返回的函数
export const getTopBannerAction = () => {
  return (dispatch) => {
    getTopBanners().then(res=>{
        // console.log(res);
        dispatch(changeTopBannerAction(res))
    })
  };
};
  export const getHotRecommendAction=(limit)=>{
    return dispatch=>{
      getHotRecommends(limit).then(res=>{
        // console.log(res);
        dispatch(changeHotRecommendAction(res))
      })
    }
  }

  export const getNewAlbumAction=(limit)=>{
    return dispatch=>{
      getNewAlbums(limit).then(res=>{
        // console.log(res)
        // const albums=res.albums
        dispatch(changeNewAlbumAction(res))
      })
    }
  }

export const getTopListAction=(id)=>{
  return dispatch=>{
    getTopList(id).then(res=>{
      // console.log(res)
      // getTopListDetail(res.list[0].id).then(res=>{
      //   dispatch(changeUpRankingAction(res.playlist.tracks));
      // })
      switch(id){
        case 19723756:
          dispatch(changeUpRankingAction(res.playlist));
          break;
        case 3779629:
          dispatch(changeNewRankingAction(res.playlist));
          break;
        case 2884035:
          dispatch(changeOriginRankingAction(res.playlist));
          break;
        default:
      }


    //  switch(id){
    //   case 19723756:
    //     getTopListDetail(res.list[0].id).then(res=>{
    //       dispatch(changeUpRankingAction(res.playlist.tracks));
    //     })
    //     break;
    //     case 3779629:
    //       getTopListDetail(res.list[1].id).then(res=>{
    //         dispatch(changeNewRankingAction(res.playlist.tracks));
    //       })
    //       break;
    //     case 2884035:
    //       getTopListDetail(res.list[2].id).then(res=>{
    //         dispatch(changeOriginRankingAction(res.playlist.tracks));
    //       })
    //       break;
    //     default:
    //  }

    })
    }
  }

